package com.fanxuankai.ksql.function.udf.datetime;

import cn.hutool.core.date.BetweenFormatter;
import cn.hutool.core.date.DateUtil;
import io.confluent.ksql.function.udf.Udf;
import io.confluent.ksql.function.udf.UdfDescription;
import io.confluent.ksql.function.udf.UdfParameter;

import java.sql.Time;
import java.sql.Timestamp;

/**
 * @author fanxuankai
 */
@UdfDescription(name = "format_between", description = "格式化时间差", author = "fanxuankai")
public class FormatBetween {
    private static final String P_LEVEL = "level";
    private static final String P_FROM = "from";
    private static final String P_TO = "to";
    private static final String P_BETWEEN_MS = "betweenMs";

    @Udf
    public String formatBetween(@UdfParameter(P_LEVEL) String level, @UdfParameter(P_FROM) Timestamp from,
                                @UdfParameter(P_TO) Timestamp to) {
        return DateUtil.formatBetween(from, to, BetweenFormatter.Level.valueOf(level));
    }

    @Udf
    public String formatBetween(@UdfParameter(P_LEVEL) String level, @UdfParameter(P_FROM) java.sql.Date from,
                                @UdfParameter(P_TO) java.sql.Date to) {
        return DateUtil.formatBetween(from, to, BetweenFormatter.Level.valueOf(level));
    }

    @Udf
    public String formatBetween(@UdfParameter(P_LEVEL) String level, @UdfParameter(P_FROM) Time from,
                                @UdfParameter(P_TO) Time to) {
        return DateUtil.formatBetween(from, to, BetweenFormatter.Level.valueOf(level));
    }

    @Udf
    public String formatBetween(@UdfParameter(P_FROM) Timestamp from,
                                @UdfParameter(P_TO) Timestamp to) {
        return DateUtil.formatBetween(from, to);
    }

    @Udf
    public String formatBetween(@UdfParameter(P_FROM) java.sql.Date from,
                                @UdfParameter(P_TO) java.sql.Date to) {
        return DateUtil.formatBetween(from, to);
    }

    @Udf
    public String formatBetween(@UdfParameter(P_FROM) Time from,
                                @UdfParameter(P_TO) Time to) {
        return DateUtil.formatBetween(from, to);
    }

    @Udf
    public String formatBetween(@UdfParameter(P_LEVEL) String level, @UdfParameter(P_BETWEEN_MS) Long betweenMs) {
        return DateUtil.formatBetween(betweenMs, BetweenFormatter.Level.valueOf(level));
    }

    @Udf
    public String formatBetween(@UdfParameter(P_BETWEEN_MS) Long betweenMs) {
        return DateUtil.formatBetween(betweenMs);
    }
}
